package com.sqfw.project.wpReport.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.sqfw.common.utils.StringUtils;
import com.sqfw.framework.web.page.PageDomain;
import com.sqfw.framework.web.page.TableSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sqfw.framework.aspectj.lang.annotation.Log;
import com.sqfw.framework.aspectj.lang.enums.BusinessType;
import com.sqfw.project.wpReport.domain.WpInfoTransfer;
import com.sqfw.project.wpReport.service.IWpInfoTransferService;
import com.sqfw.framework.web.controller.BaseController;
import com.sqfw.common.utils.poi.ExcelUtil;
import com.sqfw.framework.web.page.TableDataInfo;

/**
 * 承办单位多次处理Controller
 *
 * @author lx
 * @date 2023-07-15
 */
@RestController
@RequestMapping("/wpReport/wpInfoTransfer")
public class WpInfoTransferController extends BaseController {
    @Autowired
    private IWpInfoTransferService wpInfoTransferService;


    /**
     * 查询承办单位多次处理列表
     */
    @PreAuthorize("@ss.hasPermi('getWpInfoListTransfer')")
    @GetMapping("/getWpInfoListTransfer")
    public TableDataInfo getWpInfoListTransfer(WpInfoTransfer wpInfoTransfer) {

        List<String> wpids = wpInfoTransferService.getWpInfoListTransferList(wpInfoTransfer);
        if (StringUtils.isEmpty(wpids)) {
            return getDataTable(new ArrayList<WpInfoTransfer>());
        }

        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        List<String> wpidsCon = new ArrayList<>();
        int size = Math.min(wpids.size(), pageNum * pageSize);
        if (wpids.size() < (pageNum - 1) * pageSize) {
            return getDataTable(new ArrayList<>(), wpids);
        }
        for (int i = (pageNum - 1) * pageSize; i < size; i++) {
            wpidsCon.add(wpids.get(i));
        }

        // startPage();
        List<WpInfoTransfer> list = wpInfoTransferService.getWpInfoListTransfer(wpidsCon);

        return getDataTable(list,wpids);
    }

    /**
     * 导出承办单位多次处理列表
     */
    @Log(title = "承办单位多次处理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('getWpInfoListTransfer:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, WpInfoTransfer wpInfoTransfer) {
        List<String> wpids = wpInfoTransferService.getWpInfoListTransferList(wpInfoTransfer);
        //查询为空
        if (StringUtils.isEmpty(wpids)) {
            ExcelUtil<WpInfoTransfer> util = new ExcelUtil<WpInfoTransfer>(WpInfoTransfer.class);
            util.exportExcel(response, new ArrayList<WpInfoTransfer>(), "承办单位多次处理数据");
            return;
        }

        List<WpInfoTransfer> wpInfoTransfers = new ArrayList<>();
        for (String wpid : wpids) {
            WpInfoTransfer transfer = wpInfoTransferService.getWpInfoListTransferByWpid(wpid);
            wpInfoTransfers.add(transfer);
        }

        ExcelUtil<WpInfoTransfer> util = new ExcelUtil<WpInfoTransfer>(WpInfoTransfer.class);
        util.exportExcel(response, wpInfoTransfers, "承办单位多次处理数据");
    }


}
